import {
  fetchStreets,
  fetchUserList,
  AddCompany
} from './service';

const Model = {
  namespace: 'enterpriseandtable',
  state: {
    tableListDataSource: [],
    totalPageSize: 1,
  },
  effects: {
    // 获取企业信息表格
    *fetch({ payload }, { call, put }) {
        // Address: ""
        // Code: "21"
        // HourseArea: "21"
        // HourseIsEx: 1
        // HourseTime: "1577808000000"
        // HourseType: 1
        // Id: 1
        // Industry: "公安消防设备类"
        // IsCatalog: 0
        // IsList: 1
        // Man: "牛逼"
        // Money: 12
        // Name: "哈哈企业"
        // People: ""
        // PeopleNum: 321
        // RelAddress: "12"
        // RelDuty: "嗷嗷"
        // RelMan: "嗷嗷"
        // RelTel: "18888888"
        // Scope: ""
        // StreetId: 0
        // Time: "1579449600000"

        const response = yield call(fetchUserList, payload)
        response.List = response.List ? response.List : []
        const tableListDataSource = []
        response.List.map(item => {
          tableListDataSource.push({
            key: item.Id,
            id: item.Id,
            // 企业名称
            name: item.Name,
            // 所在街道
            street: item.Street.Name,
            // 行业类型
            industry: item.Industry,
            // 产品数量
            numble: 100,
            // 法人代表
            owner: item.Man,
            // 联系电话
            phone: item.RelTel,
            // 信息完善率
            status: Math.floor(Math.random() * 10),
            // 联系人
            contact: item.RelMan,
            // 职务
            job: item.RelDuty,
          })
        })

        const res = {
          status: 200,
          totalPageSize: response.Page.TotalCount,
          tableListDataSource
        }
        // yield put({
        //     type: 'save',
        //     payload: res,
        // });
        return res
    },

    // 获取街道信息
    *fetchStreets(_, { call }) {
      const response = yield call(fetchStreets)
      const res = {
        status: 200,
        data: response
      }
      return res

    },

    // 新增企业
    *addCompany({ payload }, { call }) {
      const param = {
        Name: payload.name,
        Industry: payload.industry,
        Man: payload.owner,
        Street: {
          Id: Number(payload.street)
        },
        StreetId: Number(payload.street),
        RelMan: payload.contact,
        RelTel: payload.phone,
        RelDuty: payload.job,
      }
      
      const res = yield call(AddCompany, param)

      if (res) {
        return 200
      }
      return 200
    },




  },
  reducers: {
    save(state, { payload }) {
        return { ...state, tableListDataSource: payload.tableListDataSource, totalPageSize: payload.totalPageSize };
    },
  },
};
export default Model;
